home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK2.toast / Development Kits (Disc 2) / QuickTime / Programming Stuff / Sample Code / Music Architecture / Mixed Bag / •Instrument Picker Test / InstrumentPickerTest.c
Encoding:
C/C++ Source or Header  |  1994-12-09  |  8.7 KB  |  436 lines  |  [TEXT/KAHL]

  1.  
  2. /*--------------------------
  3.     Inclusions
  4. --------------------------*/
  5.  
  6. #include <QuickDraw.h>
  7. #include <Windows.h>
  8. #include <OSUtils.h>
  9.  
  10. #include "BigEasy2.h"
  11. #include "BigEasyTextish.h"
  12. #include "BigEasyDialogs.h"
  13. #include <QuickTimeComponents.h>
  14.  
  15. /*--------------------------
  16.     Limits and Konstants
  17. --------------------------*/
  18. enum
  19.     {
  20.     mOpen = 100,
  21.     mFirstEditField,
  22.     mEditSynthesizerType,
  23.     mEditSynthesizerName,
  24.     mEditInstrumentName,
  25.     mEditInstrumentNumber,
  26.     mEditGMNumber,
  27.  
  28.     mNewNoteChannel,
  29.  
  30.     mPickInstrument,
  31.     mPickInstrumentNoMix,
  32.     mPickInstrumentSameSynth,
  33.     mPickInstrumentFascist,
  34.  
  35.     mClose
  36.     };
  37.  
  38. typedef struct
  39.     {
  40.     WindowPtr w;
  41.  
  42.     NoteAllocator na;
  43.     ToneDescription td;
  44.     } Globals;
  45.  
  46. Globals g;
  47.  
  48.  
  49. /*--------------------------
  50.     Prototypes
  51. --------------------------*/
  52. static void DrawDoc(short n);
  53. static void ClickDoc(short n,Point p,short mods);
  54. static void KeyDoc(short n,short key,short code,short mods);
  55. static void IdleDoc(short n, Boolean front);
  56. static void GoAwayDoc(short n);
  57. static void ActivateDoc(short n);
  58. static void DeactivateDoc(short n);
  59. static void LetsQuit(short n,short menuItem,short menuRef);
  60. static void EditFieldMenu(short n,short menuItem,short menuRef);
  61. static void DocNoteChannel(short n,short menuItem,short menuRef);
  62. static void Pick(short n,short menuItem,short menuRef);
  63. static void OpenWindow(short n,short menuItem,short menuRef);
  64. static void InitVars(void);
  65. static void EditField(short f);
  66. static void InvalFields(void);
  67.  
  68. static pascal Boolean MyFilterProc(DialogPtr theDialog,
  69.         EventRecord *theEvent,short *itemHit);
  70.         
  71. /*--------------------------
  72.     Computer Programs
  73. --------------------------*/
  74.  
  75. #define kLineHeight 12
  76.  
  77. #define kLine1 30
  78. #define kLine2 42
  79. #define kLine3 54
  80. #define kLine4 66
  81. #define kLine5 78
  82.  
  83. #define kCenterLine 125
  84.  
  85. #define kWindowWidth (kCenterLine*2)
  86. #define kWindowHeight (kLine5 + 10)
  87.  
  88. #define kHeaderHeight 17
  89.  
  90. void DrawDoc(short n)
  91. /*
  92.  * Draws the window.
  93.  */
  94.     {
  95.     #pragma unused (n)
  96.  
  97.     EraseRect(&gBigRect);
  98.     TextSize(9);
  99.     MoveTo(kCenterLine,12);
  100.     DrawStringCenter("\pTone Description");
  101.     MoveTo(0,kHeaderHeight-3);
  102.     Line(9000,0);
  103.     MoveTo(0,kHeaderHeight-1);
  104.     Line(9000,0);
  105.  
  106. /* ---------------- */
  107.     MoveTo(kCenterLine,kLine1);
  108.     DrawStringRight("\pSynthesizer Type: ");
  109.     if(g.td.synthesizerType)
  110.         {
  111.         DrawChar(g.td.synthesizerType>>24);
  112.         DrawChar((g.td.synthesizerType>>16) & 0xFF);
  113.         DrawChar((g.td.synthesizerType>>8) & 0xFF);
  114.         DrawChar((g.td.synthesizerType) & 0xFF);
  115.         }
  116.     else
  117.         DrawString("\p0");
  118.  
  119.  
  120. /* ---------------- */
  121.     MoveTo(kCenterLine,kLine2);
  122.     DrawStringRight("\pSynthesizer Name: ");
  123.     DrawString(g.td.synthesizerName);
  124.  
  125. /* ---------------- */
  126.     MoveTo(kCenterLine,kLine3);
  127.     DrawStringRight("\pInstrument Name: ");
  128.     DrawString(g.td.instrumentName);
  129.  
  130. /* ---------------- */
  131.     MoveTo(kCenterLine,kLine4);
  132.     DrawStringRight("\pInstrument Number: ");
  133.     DrawNum(g.td.instrumentNumber);
  134.  
  135. /* ---------------- */
  136.     MoveTo(kCenterLine,kLine5);
  137.     DrawStringRight("\pGM Number: ");
  138.     DrawNum(g.td.gmNumber);
  139.     }
  140.  
  141.  
  142. static void Pick(short n,short menuItem,short menuRef)
  143.     {
  144.     unsigned long flags;
  145.     ComponentResult result;
  146.  
  147.     if(menuRef == mPickInstrumentNoMix)
  148.         flags = 1;
  149.     else if(menuRef == mPickInstrumentSameSynth)
  150.         flags = 2;
  151.     else if(menuRef == mPickInstrumentFascist)
  152.         flags = 3;
  153.     else
  154.         flags = 0;
  155.  
  156.  
  157.     result = NAPickInstrument(g.na,MyFilterProc,"\pPick An Instrument:",&g.td,
  158.             flags,0,0,0);
  159.     InvalFields();
  160.     }
  161.  
  162. void ClickDoc(short n,Point p,short mods)
  163. /*
  164.  * Come here for a click in the window.
  165.  */
  166.     {
  167.     Str31 s;
  168.  
  169.  
  170.  
  171.     if(p.v < kLine1 && p.v > kLine1 - kLineHeight)
  172.         EditField(1);
  173.     else if(p.v < kLine2 && p.v > kLine2 - kLineHeight)
  174.         EditField(2);
  175.     else if(p.v < kLine3 && p.v > kLine3 - kLineHeight)
  176.         EditField(3);
  177.     else if(p.v < kLine4 && p.v > kLine4 - kLineHeight)
  178.         EditField(4);
  179.     else if(p.v < kLine5 && p.v > kLine5 - kLineHeight)
  180.         EditField(5);
  181.     else
  182.         Pick(0,0,0);
  183.     }
  184.  
  185.  
  186. void EditField(short f)
  187. /*
  188.  * Edit a field, 1-5.
  189.  */
  190.     {
  191.     Str31 s;
  192.  
  193.  
  194.     switch(f)
  195.         {
  196.         case 1:
  197.             if(g.td.synthesizerType)
  198.                 {
  199.                 s[0] = 4;
  200.                 s[1] = g.td.synthesizerType >> 24;
  201.                 s[2] = g.td.synthesizerType >> 16;
  202.                 s[3] = g.td.synthesizerType >> 8;
  203.                 s[4] = g.td.synthesizerType;
  204.                 }
  205.             else
  206.                 {
  207.                 s[0] = 1;
  208.                 s[1] = '0';
  209.                 }
  210.             EasyDialogGetString("\pTone Description",
  211.                     "\pNew value for synthesizer type:",
  212.                     s,4);
  213.             if(s[0] == 1 && s[1] == '0')
  214.                 g.td.synthesizerType = 0;
  215.             else
  216.                 {
  217.                 while(s[0] < 4)
  218.                     s[++s[0]] = ' ';
  219.                 g.td.synthesizerType = (((unsigned long)s[1]) << 24)
  220.                         + (((unsigned long)s[2]) << 16)
  221.                         + (((unsigned long)s[3]) << 8)
  222.                         + (((unsigned long)s[4]));
  223.                 }
  224.             goto inval;
  225.  
  226.         case 2:
  227.             EasyDialogGetString("\pTone Description",
  228.                     "\pNew value for synthesizer name:",
  229.                     g.td.synthesizerName,31);
  230.             goto inval;
  231.  
  232.         case 3:
  233.             EasyDialogGetString("\pTone Description",
  234.                     "\pNew value for instrument name:",
  235.                     g.td.instrumentName,31);
  236.             goto inval;
  237.  
  238.         case 4:
  239.             EasyDialogGetNumber("\pTone Description",
  240.                     "\pNew value for instrument number:",
  241.                     &g.td.instrumentNumber);
  242.             goto inval;
  243.  
  244.         case 5:
  245.             EasyDialogGetNumber("\pTone Description",
  246.                     "\pNew value for GM number:",
  247.                     &g.td.gmNumber);
  248.     inval:
  249.             InvalFields();
  250.             break;
  251.         }
  252.     }
  253.  
  254. void InvalFields(void)
  255.     {
  256.     Rect r;
  257.  
  258.     r.top = kHeaderHeight;
  259.     r.left = kCenterLine;
  260.     r.right = kWindowWidth;
  261.     r.bottom = kWindowHeight;
  262.  
  263.     SetPort(g.w);
  264.     InvalRect(&r);
  265.     }
  266.  
  267. void KeyDoc(short n,short key,short code,short mods)
  268.     {
  269.     #pragma unused (n,key,code,mods)
  270.  
  271.     if(key == ' ' || key == 13 || key == 3)
  272.         Pick(0,0,0);
  273.     else if(key >= '1' && key <= '5')
  274.         EditField(key-'0');
  275.     }
  276.  
  277. void IdleDoc(short n, Boolean front)
  278.     {
  279.     #pragma unused (n,front)
  280.     }
  281.  
  282. void GoAwayDoc(short n)
  283. /*
  284.  * Close that window...
  285.  */
  286.     {
  287.     UninstallWindow(n);
  288.     }
  289.  
  290. void ActivateDoc(short n)
  291.     {
  292. #pragma unused (n)
  293.      SetMenuItem(mClose,1,0,0,nil);                /* enable "Close" menu item        */
  294.     SetMenuItem(mOpen,-1,0,0,nil);                /* disable "Open" menu item        */
  295.     }
  296.  
  297. void DeactivateDoc(short n)
  298.     {
  299. #pragma unused (n)
  300.     SetMenuItem(mClose,-1,0,0,nil);                /* disable "Close" menu item    */
  301.     SetMenuItem(mOpen,1,0,0,nil);                /* enable "Open" menu item        */
  302.     }
  303.  
  304. void LetsQuit(short n,short menuItem,short menuRef)
  305.     {
  306. #pragma unused (n,menuItem,menuRef)
  307.     gQuitApp++;
  308.     }
  309.  
  310. void EditFieldMenu(short n,short menuItem,short menuRef)
  311.     {
  312.     EditField(menuRef - mFirstEditField);
  313.     }
  314.  
  315.  
  316. static void DocNoteChannel(short n,short menuItem,short menuRef)
  317.     {
  318.     NoteChannel nc;
  319.     long dummy;
  320.     short p;
  321.     ComponentResult thisError;
  322.  
  323.     NoteRequest nr;
  324.  
  325.     nr.tone = g.td;
  326.     nr.polyphony = 4;
  327.     nr.typicalPolyphony = 0x00010000;
  328.  
  329.     thisError = NANewNoteChannel(g.na,&nr,&nc);
  330.     p = 36;
  331.     while(!Button())
  332.         {
  333.         NAPlayNote(g.na,nc,p,128);
  334.         Delay(15,&dummy);
  335.         NAPlayNote(g.na,nc,p,0);
  336.         p++;
  337.         }
  338.  
  339.     thisError = NADisposeNoteChannel(g.na,nc);
  340.     }
  341.  
  342.  
  343.  
  344.  
  345. void OpenWindow(short n,short menuItem,short menuRef)
  346.     {
  347. #pragma unused (n,menuItem,menuRef)
  348.     Rect r;
  349.  
  350.     SetRect(&r,0,0,kWindowWidth,kWindowHeight);
  351.     OffsetRect(&r,20,40);
  352.  
  353.     g.w = InstallWindow(1,"\p",&r,0,wCopyDraw,
  354.             DrawDoc,ClickDoc,KeyDoc,nil,
  355.             ActivateDoc,DeactivateDoc,IdleDoc);
  356.     }
  357.  
  358.  
  359. #ifdef RegisterLocalComponents
  360. Component RegisterNoteAllocator(void);
  361. #endif
  362.  
  363. void InitVars()
  364. /*
  365.  * Called once at startup: yes, it
  366.  * inits the vars.
  367.  */
  368.     {
  369.     ComponentResult result;
  370.  
  371.     g.na = OpenDefaultComponent('nota',0);
  372.  
  373.     g.td.synthesizerType = 0;
  374.     g.td.synthesizerName[0] = 0;
  375.     g.td.instrumentName[0] = 0;
  376.     g.td.instrumentNumber = 0;
  377.     g.td.gmNumber = 0;
  378.  
  379. #ifdef RegisterLocalComponents
  380.     RegisterNoteAllocator();
  381. #endif
  382.  
  383.     }
  384.  
  385. void Bootstrap()
  386.     {
  387.     InitVars();
  388.  
  389. /*** File Menu ***/
  390.     InstallMenu("\pFile",nil,0);
  391.     InstallMenuItem("\pQuit/Q",LetsQuit,0);
  392.  
  393. /*** Edit Menu ***/
  394.     InstallEditMenu(nil,nil,nil,nil,nil);
  395.  
  396. /*** Picker Menu ***/
  397.     InstallMenu("\pPicker",nil,0);
  398.     InstallMenuItem("\pSynthesizer Type…/1",EditFieldMenu,mEditSynthesizerType);
  399.     InstallMenuItem("\pSynthesizer Name…/2",EditFieldMenu,mEditSynthesizerName);
  400.     InstallMenuItem("\pInstrument Name…/3",EditFieldMenu,mEditInstrumentName);
  401.     InstallMenuItem("\pInstrument Number…/4",EditFieldMenu,mEditInstrumentNumber);
  402.     InstallMenuItem("\pGM Number…/5",EditFieldMenu,mEditGMNumber);
  403.     InstallMenuItem("\p(-",0,0);
  404.     InstallMenuItem("\pPick Instrument…/P",Pick,mPickInstrument);
  405.     InstallMenuItem("\pPick Instrument… [No Mix]/7",Pick,mPickInstrumentNoMix);
  406.     InstallMenuItem("\pPick Instrument… [Same Synth]/8",Pick,mPickInstrumentSameSynth);
  407.     InstallMenuItem("\pPick Instrument… [Fascist]/9",Pick,mPickInstrumentFascist);
  408.     InstallMenuItem("\p(-",0,0);
  409.     InstallMenuItem("\pNew Note Channel/N",DocNoteChannel,mNewNoteChannel);
  410.  
  411.     OpenWindow(0,0,0);
  412.     }
  413.  
  414. void Hatstrap()
  415. /*
  416.   * clean up
  417.   */
  418.     {
  419.     }
  420.  
  421.  
  422. pascal Boolean MyFilterProc(DialogPtr theDialog,
  423.         EventRecord *theEvent,short *itemHit)
  424.     {
  425.     GrafPort *oldPort;
  426.  
  427.     GetPort(&oldPort);
  428.     SetPort(theDialog);
  429.  
  430.     if(theEvent->what == updateEvt && (DialogPtr)theEvent->message != theDialog)
  431.             HandleUpdateEvent(theEvent);
  432.  
  433.     SetPort(oldPort);
  434.     return false;
  435.     }
  436.